package com.gwkj.zxjl_user.AccountDB;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gwkj.zxjl_user.Bean.Login.AccountEntity;
import com.gwkj.zxjl_user.Bean.Other.DeviceEntity;

/**
 * Created by fujh on 16/2/3.
 */
public class ADBService {

    private ADBHelper mHelper = null;

    public ADBService(Context context)
    {
        //通过静态方法来获取DBhelper实例
        mHelper = ADBHelper.getInstanceDBHelper(context);
    }

    /**
     * 插入
     * worker_id integer, mobile text, session_id text
     * @param account
     */
    public synchronized void addAccount(AccountEntity account)
    {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.execSQL("insert into account_info(worker_id, mobile, session_id) values(?,?,?)",
                new Object[]{account.getWorker_id(), account.getMobile(), account.getSession_id()});
    }


    public synchronized void deleteAccount()
    {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.execSQL("delete from account_info", new Object[]{});
    }


    public synchronized void updateAccount(AccountEntity account)
    {
        deleteAccount();
        addAccount(account);
    }

    public synchronized AccountEntity getAccount()
    {
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from account_info", new String[]{});
        AccountEntity account = new AccountEntity();
        while (cursor.moveToNext())
        {
            account.setWorker_id(cursor.getInt(1));
            account.setMobile(cursor.getString(2));
            account.setSession_id(cursor.getString(3));
            break;
        }
        cursor.close();
        return account;
    }

    public synchronized String getToken()
    {
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from account_info", new String[]{});
        String token = "";
        if (cursor.moveToNext()) {
            token = cursor.getString(3);
        }
        cursor.close();
        return token;
    }

    public synchronized int getID()
    {
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from account_info", new String[]{});
        int id = 0;
        if (cursor.moveToNext()) {
            id = cursor.getInt(1);
        }
        cursor.close();
        return id;
    }

    public synchronized boolean isExistAct()
    {
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from account_info", new String[]{});
        boolean exists = cursor.moveToNext();
        cursor.close();
        return exists;
    }

    /**
     *
     * @param device
     */
    public synchronized void addDevice(DeviceEntity device)
    {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.execSQL("insert into device_info(platform, version, app, id, info) values(?,?,?,?,?)",
                new Object[]{device.getPlatform(), device.getVersion(), device.getApp(), device.getDevice_id(), device.getDevice_info()});
        //db.close();
    }

    public synchronized void deleteDevice()
    {
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.execSQL("delete from device_info", new Object[]{});
        //db.close();
    }

    public synchronized void updateDevice(DeviceEntity device)
    {
        deleteDevice();
        addDevice(device);
    }

    public synchronized DeviceEntity getDevice()
    {
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from device_info", new String[]{});
        DeviceEntity device = new DeviceEntity();
        if (cursor.moveToNext())
        {
            device.setPlatform(cursor.getString(1));
            device.setVersion(cursor.getString(2));
            device.setApp(cursor.getString(3));
            device.setDevice_id(cursor.getString(4));
            device.setDevice_info(cursor.getString(5));
        }
        cursor.close();
        return device;
    }
}
